﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;
using Konekcija.Core.Reporting.Dto.Application.Security;

namespace Konekcija.Core.Reporting.Mappings.Security
{
    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Schema("security");
            Table("Users");

            Id(x => x.Id).GeneratedBy.GuidComb().Not.Nullable();
            Map(x => x.Username).Nullable();
            Map(x => x.Password).Nullable();
            Map(x => x.FirstName).Nullable();
            Map(x => x.Insertion).Nullable();
            Map(x => x.LastName).Nullable();
            Map(x => x.Sex).Nullable();
            Map(x => x.StreetName).Nullable();
            Map(x => x.ZipCode).Nullable();
            Map(x => x.ZipCodeLetters).Nullable();
            Map(x => x.City).Nullable();
            Map(x => x.Province).Nullable();
            Map(x => x.Country).Nullable();
            Map(x => x.Email).Nullable();
            Map(x => x.Phone).Nullable();
            Map(x => x.Mobilenumber).Nullable();
            Map(x => x.Website).Nullable();
            Map(x => x.PhotoUrl).Nullable();
            Map(x => x.JobTitle).Nullable();
            Map(x => x.WorkExperience).Nullable();
            Map(x => x.CareerStatus).Nullable();
            Map(x => x.CareerLevel).Nullable();
            Map(x => x.Industry).Nullable();
            Map(x => x.PersonalStatement).Nullable();
            Map(x => x.ContactPreference).Nullable();
            Map(x => x.Active).Nullable();
            Map(x => x.Newsletter).Nullable();
            Map(x => x.FoundVia).Nullable();
            Map(x => x.DateCreated).Nullable();
            Map(x => x.DateChanged).Nullable();
            Map(x => x.ByUserId).Nullable();


            // security
            HasManyToMany(x => x.Groups)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .ParentKeyColumn("UserId")
                .ChildKeyColumn("GroupId")
                .Table("security.UsersToGroups");

            // crm
            HasManyToMany(x => x.Products)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .ParentKeyColumn("UserId")
                .ChildKeyColumn("ProductId")
                .Table("security.UsersToProducts");

            HasManyToMany(x => x.Accounts)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .ParentKeyColumn("UserId")
                .ChildKeyColumn("AccountId")
                .Table("security.UsersToAccounts");

            HasManyToMany(x => x.Merchants)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .ParentKeyColumn("UserId")
                .ChildKeyColumn("MerchantId")
                .Table("security.UsersToMerchants");

            // profile
            HasMany(x => x.CommunicationChannels)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .Key(k => k.Not.Nullable())
                .KeyColumn("UserId");

            HasMany(x => x.Feedbacks)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .Key(k => k.Not.Nullable())
                .KeyColumn("UserId");

            HasMany(x => x.Notifications)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .Key(k => k.Not.Nullable())
                .KeyColumn("UserId");

            HasMany(x => x.Stats)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .Key(k => k.Not.Nullable())
                .KeyColumn("UserId");
        }
    }
}
